/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Service;

import Exception.InvalidRequestParameterException;
import Util.TranslatorUtils;
import Util.ValidatorUtils;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import th.co.tac.esv.pre.ejb.dat.balance.transfer.BalanceTransferRequest;
import th.co.tac.esv.pre.ejb.dat.balance.transfer.BalanceTransferResponse;
import th.co.tac.esv.pre.ejb.itf.balance.transfer.BalanceTransferBeanRemote;

/**
 *
 * @author Peat
 */
public class Balance2TransferService extends BaseService {

    public BalanceTransferResponse balance2transfer(
            HttpServletRequest httpRequest) throws NamingException
    {
        this.readyEJB(null);

        if (!cache.containsKey("BalanceTransferResponse")) {
            BalanceTransferRequest reqs = new BalanceTransferRequest();
            reqs.setUserName((String) httpRequest.getSession().getAttribute("InfoSubscriber"));
            reqs.setSubrNumb((String) httpRequest.getSession().getAttribute("InfoSubscriber"));
            reqs.setIpAddress(httpRequest.getRemoteAddr());
            reqs.setLang(TranslatorUtils.getBackendLang(httpRequest));
            reqs.setFuncID("");
            reqs.setMenuID("");

            BalanceTransferBeanRemote bean = (BalanceTransferBeanRemote)
                    ic.lookup("JNDIBalanceTransferBean");
            BalanceTransferResponse resp = bean.getCallBalance2Transfer(reqs);

            cache.put("BalanceTransferResponse", resp);
        }
        return (BalanceTransferResponse) cache.get("BalanceTransferResponse");
    }

    public BalanceTransferResponse confirm2transfer(
            HttpServletRequest httpRequest) throws NamingException, InvalidRequestParameterException
    {
        this.readyEJB(null);

        if (!cache.containsKey("ConfirmTransferResponse")) {
            BalanceTransferRequest reqs = new BalanceTransferRequest();
            reqs.setUserName((String) httpRequest.getSession().getAttribute("InfoSubscriber"));
            reqs.setSubrNumb((String) httpRequest.getSession().getAttribute("InfoSubscriber"));
            reqs.setIpAddress(httpRequest.getRemoteAddr());
            reqs.setLang(TranslatorUtils.getBackendLang(httpRequest));
            reqs.setFuncID("");
            reqs.setMenuID("");

            // REQUEST PARAMETERS
            // FIXME: CONVERT TO 08 -- GET FROM USERNAME
            // required 66
            ValidatorUtils.SECURITY_TYPE[] contraints = {
                ValidatorUtils.SECURITY_TYPE.NUMBER_VALUE.set("amt", false, 20, 200),
                ValidatorUtils.SECURITY_TYPE.MOBILE66.set("sourceno", false),
                ValidatorUtils.SECURITY_TYPE.MOBILE66.set("sourcedest", false)
            };
            if (!ValidatorUtils.isValid(httpRequest, contraints)) {
                throw new InvalidRequestParameterException("Bad Request due to invalid parameter");
            }
            
            reqs.setSourceno(httpRequest.getParameter("sourceno"));
            reqs.setSourcedest(httpRequest.getParameter("sourcedest"));
            // required 20 - 200
            reqs.setAmttransfer(httpRequest.getParameter("amt"));


            BalanceTransferBeanRemote bean = (BalanceTransferBeanRemote)
                    ic.lookup("JNDIBalanceTransferBean");
            BalanceTransferResponse resp = bean.getCallConfirm2Transfer(reqs);

            cache.put("ConfirmTransferResponse", resp);
        }
        return (BalanceTransferResponse) cache.get("ConfirmTransferResponse");
    }

    public BalanceTransferResponse result2transfer(
            HttpServletRequest httpRequest) throws NamingException, InvalidRequestParameterException
    {
        this.readyEJB(null);

        if (!cache.containsKey("ResultTransferResponse")) {
            BalanceTransferRequest reqs = new BalanceTransferRequest();
            reqs.setUserName((String) httpRequest.getSession().getAttribute("InfoSubscriber"));
            reqs.setSubrNumb((String) httpRequest.getSession().getAttribute("InfoSubscriber"));
            reqs.setIpAddress(httpRequest.getRemoteAddr());
            reqs.setLang(TranslatorUtils.getBackendLang(httpRequest));
            reqs.setFuncID("");
            reqs.setMenuID("");

            ValidatorUtils.SECURITY_TYPE[] contraints = {
                ValidatorUtils.SECURITY_TYPE.NUMBER_VALUE.set("amt", false, 20, 200),
                ValidatorUtils.SECURITY_TYPE.MOBILE66.set("sourcedest", false)
            };
            if (!ValidatorUtils.isValid(httpRequest, contraints)) {
                throw new InvalidRequestParameterException("Bad Request due to invalid parameter");
            }

            // REQUEST PARAMETERS
            reqs.setToPhoneNo(httpRequest.getParameter("sourcedest"));
            reqs.setBalanceAmt(httpRequest.getParameter("amt"));

            BalanceTransferBeanRemote bean = (BalanceTransferBeanRemote)
                    ic.lookup("JNDIBalanceTransferBean");
            BalanceTransferResponse resp = bean.getCallResult2Transfer(reqs);

            cache.put("ResultTransferResponse", resp);
        }
        return (BalanceTransferResponse) cache.get("ResultTransferResponse");
    }
}

